package com.scalar.chiptrack.operations.tapereel.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.dao.NewVendorLocationDAO;
import com.scalar.chiptrack.operations.release.forms.ReleaseInfoSetupForm;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.operations.release.ContactPerson;
import com.scalar.chiptrack.operations.release.ReleaseHelper;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 16, 2007
 * Time: 6:23:47 PM
 */
public class TapeReelRequestViewCommand  extends ActionCommand
{

     private static final String ACTION_NAME = "/tapeReelRequest.do";


    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws ScalarException
    {
        TapeReelRequestForm tapeReelRequestForm     = ( TapeReelRequestForm ) form;
        TapeReelRequest tapeReelRequest             = tapeReelRequestForm.getTapeReelRequest();
        ActionForward actionForward                 = mapping.findForward( "view" );
        HttpSession mSession                        = request.getSession();
        User user                                   = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );
        HashMap<String,String> errors                              = new HashMap<String,String>();
        HashMap<String,String> info                                = new HashMap<String,String>();
        int tapeReelRequestId                       = -1;
        request.setAttribute( INFO_MESSAGES, info );
        request.setAttribute( ERROR_MESSAGES, errors );

        if ( user == null )
        {
            mSession.setAttribute ( SessionKeys.SOURCE_PAGE_KEY,  ACTION_NAME );
            return ( mapping.findForward ( "login" ) );
        }
        else
        {
            if ( user.isActionAllowed ( Functionality.TAPE_REEL_REQUEST , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }
      try
      {
          try
          {
              tapeReelRequestId = Integer.parseInt(request.getParameter("trReqId"));
          }
          catch (NumberFormatException nfe)
          {
              tapeReelRequestId = -1;
          }

          tapeReelRequest.setTrRequestid(tapeReelRequestId);
          tapeReelRequest = TapeReelRequestDAO.loadTapeReelRerquestDetails(null, tapeReelRequest);
          tapeReelRequest.setLotList( TapeReelRequestDAO.getFGLotDetails( tapeReelRequest ) );
          tapeReelRequest.setPinCount( TapeReelRequestDAO.getProcessParameter( tapeReelRequest.getTrProcessStepId(), "PIN" ));
          tapeReelRequest.setPackageType( TapeReelRequestDAO.getProcessParameter( tapeReelRequest.getTrProcessStepId(), "PACKAGE ID" ));

          ReleaseInfoSetupForm releaseInfoSetupForm = null;
          releaseInfoSetupForm = new ReleaseInfoSetupForm();
          releaseInfoSetupForm.setVendorId(tapeReelRequest.getVendorId());
          releaseInfoSetupForm.setCategory("PROD");
          RelaseDAO.loadReleaseInfoSetup("Class Store", releaseInfoSetupForm);

          ArrayList<ContactPerson> vendorContactPersonsList = RelaseDAO.loadVendorContactPersonsList(null, releaseInfoSetupForm.getReleaseInfoSetupId());
          tapeReelRequest.setTrRequestAlerts(ReleaseHelper.getContactPersonsAsString("email", vendorContactPersonsList));
          ArrayList<ContactPerson> contactPersonsList = RelaseDAO.loadContactPersonsList(releaseInfoSetupForm.getReleaseInfoSetupId());
          tapeReelRequest.setInternalContactPersonEmail(ReleaseHelper.getContactPersonsAsString("email", contactPersonsList));
          tapeReelRequest.setVendorContactPersonsList(vendorContactPersonsList);
          tapeReelRequest.setContactPersonsList(contactPersonsList);
          tapeReelRequestForm.setTapeReelRequest( tapeReelRequest );
      }
      catch (Exception e)
      {
          e.printStackTrace();
          errors.put("Could not retrive the Tape & Reel Request", null);
          request.setAttribute("partNumbersList", TapeReelRequestDAO.loadTapeAndReelFgProducts() );
          request.setAttribute("vendorsList", TapeReelRequestDAO.getTrLocations());
          request.setAttribute("statusList", TapeReelRequest.getStatusList());
          tapeReelRequest.setReqStatus("Issued_Pulled");
          actionForward = mapping.findForward( "search" );
      }
      return actionForward;
    }
}
